﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmDMKhachHang

    Private _objKH As DM_KhachHang
    Public Property objKH() As DM_KhachHang
        Get
            Return _objKH
        End Get
        Set(ByVal value As DM_KhachHang)
            _objKH = value
        End Set
    End Property

    Private _LaChon As Boolean
    Public Property LaChon() As Boolean
        Get
            Return _LaChon
        End Get
        Set(ByVal value As Boolean)
            _LaChon = value
        End Set
    End Property

    Private _MaKhachHang As String
    Public Property MaKhachHang() As String
        Get
            Return _MaKhachHang
        End Get
        Set(ByVal value As String)
            _MaKhachHang = value
        End Set
    End Property

    Private _IDKhachHang As String
    Public Property IDKhachHang() As String
        Get
            Return _IDKhachHang
        End Get
        Set(ByVal value As String)
            _IDKhachHang = value
        End Set
    End Property

    Private Sub FrmDMKhachHang_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        If e.KeyCode = Keys.Enter And LaChon And grvKhachHang.FocusedRowHandle >= 0 Then
            btnChon_ItemClick(Nothing, Nothing)
        End If
        'Thêm mới
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pADD, btnThemMoi) Then
            btnThemMoi_ItemClick(Nothing, Nothing)
        End If
        'Sửa đổi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEDIT, btnSuaDoi) Then
            btnSuaDoi_ItemClick(Nothing, Nothing)
        End If
        'Xóa
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pDELETE, btnXoa) Then
            btnXoa_ItemClick(Nothing, Nothing)
        End If
        'Xuất
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXPORT, btnXuatExcel) Then
            btnXuatExcel_ItemClick(Nothing, Nothing)
        End If
        'Nạp lại
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pREFRESH, btnNapLai) Then
            btnNapLai_ItemClick(Nothing, Nothing)
        End If

    End Sub
    Public Shared Them, Sua, Xoa, SaoChep As Boolean
    Private Sub PhanQuyen()
        btnThemMoi.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSaoChep.Visibility = IIf(SaoChep, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSuaDoi.Visibility = IIf(Sua, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnXoa.Visibility = IIf(Xoa, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
    End Sub
    Private Sub FrmDMKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnThemMoi.Caption = BtnAddNewCaption
        btnSuaDoi.Caption = BtnEditCaption
        btnXoa.Caption = BtnDeleteCaption
        btnThoat.Caption = BtnExitCaption
        btnSaoChep.Caption = BtnCopyCaption
        btnXuatExcel.Caption = BtnExportDataCaption
        btnNapLai.Caption = BtnRefreshCaption
        If Not LaChon Then
            btnChon.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        End If
        GroupControl1.Text = GetLabelPhimTatCoBan("", True, True, True)

        FillDataToGrid(False)
        SetFilterConditionMa(grvKhachHang, grdColMaKhachHang, grdColMaKhachHang.FieldName, MaKhachHang, LaChon)
        PhanQuyen()
    End Sub
    Private Sub FillDataToGrid(Optional ByVal LoadDataFromDB As Boolean = True)
        If LoadDataFromDB OrElse lstSourceAllSanPham.Count = 0 Then
            ConnectToDB()
            lstSourceAllKhachHang = GetAll_DMKhachHang()
        End If

        grdKhachHang.DataSource = lstSourceAllKhachHang
        grdKhachHang.RefreshDataSource()
    End Sub

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub btnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemMoi.ItemClick
        Dim frm As New FrmCapNhatKhachHang
        frm.Action = vAddAction
        frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
        frm.ShowDialog()
        FillDataToGrid(False)
    End Sub

    Private Sub btnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoa.ItemClick
        Dim strID As String = grvKhachHang.GetRowCellValue(grvKhachHang.FocusedRowHandle, grdColID).ToString
        If String.IsNullOrEmpty(strID) Then
            MsgInfo("Bạn chưa chọn khách hàng")
            Return
        End If

        'Dim objHDNhap = (From ds In dbTV.HDNhaps Where ds.InUsed AndAlso ds.IDKhachHang = strID Select ds).FirstOrDefault
        'If objHDNhap IsNot Nothing Then
        '    MsgInfo("Tồn tại dữ liệu hóa đơn nhập")
        '    Return
        'End If
        'Dim objHDXuat = (From ds In dbTV.HDXuats Where ds.InUsed AndAlso ds.IDKhachHang = strID Select ds).FirstOrDefault
        'If objHDXuat IsNot Nothing Then
        '    MsgInfo("Tồn tại dữ liệu hóa đơn xuất")
        '    Return
        'End If

        If MsgAskDelete("Bạn có muốn xóa khách hàng này không?") + vMsg.vReSult.Yes Then
            Dim objKH = (From ds In dbTV.DM_KhachHangs Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
            If objKH IsNot Nothing Then
                objKH.EditedOn = dbTV.GetDateTimeServer
                objKH.InUsed = False
                iSubmitChange()
                Dim itemRemove As New clsDMKhachHang
                itemRemove = (From ds In lstSourceAllKhachHang Where ds.ID = strID Select ds).FirstOrDefault
                lstSourceAllKhachHang.Remove(itemRemove)
                FillDataToGrid(False)
            End If
        End If
    End Sub

    Private Sub btnNapLai_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNapLai.ItemClick
        FillDataToGrid(True)
    End Sub

    Private Sub btnSaoChep_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSaoChep.ItemClick
        Dim strID As String = grvKhachHang.GetRowCellValue(grvKhachHang.FocusedRowHandle, grdColID).ToString
        If String.IsNullOrEmpty(strID) Then
            MsgInfo("Bạn chưa chọn khách hàng")
            Return
        End If

        Dim objKH = (From ds In dbTV.DM_KhachHangs Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
        Dim frm As New FrmCapNhatKhachHang
        frm.KhachHangEdit = objKH
        frm.Action = vCopyAction
        frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
        frm.ShowDialog()
        FillDataToGrid(False)
    End Sub

    Private Sub btnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSuaDoi.ItemClick
        Dim strID As String = grvKhachHang.GetRowCellValue(grvKhachHang.FocusedRowHandle, grdColID).ToString
        Dim objKH = (From ds In dbTV.DM_KhachHangs Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
        If String.IsNullOrEmpty(strID) Then
            MsgInfo("Bạn chưa chọn khách hàng")
            Return
        End If
        Dim frm As New FrmCapNhatKhachHang
        frm.KhachHangEdit = objKH
        frm.IDKhachHang = strID
        frm.Action = vEditAction
        frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
        frm.ShowDialog()
        FillDataToGrid(False)
    End Sub

    Private Sub btnXuatExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuatExcel.ItemClick
        Export_Dialog(SaveFileDialog1, grdKhachHang)
    End Sub

    Private Sub btnChon_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnChon.ItemClick
        Dim strID As String = grvKhachHang.GetRowCellValue(grvKhachHang.FocusedRowHandle, grdColID).ToString
        If String.IsNullOrEmpty(strID) Then
            MsgInfo("Bạn chưa chọn khách hàng")
            Return
        End If
        IDKhachHang = strID
        objKH = (From ds In dbTV.DM_KhachHangs Where ds.InUsed AndAlso ds.ID = CStr(strID) Select ds).FirstOrDefault
        Me.Close()
    End Sub
End Class